/**
 * 
 */
package net.toocruel.iqismart.service.impl;


import net.toocruel.iqismart.dao.setting.*;
import net.toocruel.iqismart.entity.dto.SettingCreditAndCoinInfo;
import net.toocruel.iqismart.entity.setting.*;
import net.toocruel.iqismart.service.SettingService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author toocruel
 *
 */
@Service
@Transactional
public class SettingServiceImpl implements SettingService {

	@Autowired
	private SettingRepository settingRepository;

	@Autowired
	private SettingWebsiteRepository settingWebsiteRepository;

	@Autowired
	private SettingEmailRepository settingEmailRepository;

    @Autowired
    private SettingCoinRepository settingCoinRepository;

    @Autowired
    private SettingCreditRepository settingCreditRepository;

    @Autowired
    private SettingSearchRepository settingSearchRepository;

    @Autowired
    private SettingSeoRepository settingSeoRepository;

    @Autowired
    private SettingOauthRepository settingOauthRepository;

    @Autowired
    private SettingIrrigationRepository settingIrrigationRepository;

    @Autowired
    private SettingTimeRepository settingTimeRepository;

    @Autowired
    private SettingRegisterRepository settingRegisterRepository;


	@Override
	public List<Setting> findAll() {
		return settingRepository.findAll();
	}

	@Override
	public Setting getSetting() {
		try {
			List<Setting> all = this.findAll();
			if(all.isEmpty()){
				Setting setting = new Setting();
				settingRepository.save(setting);
				return setting;
			}
			return all.get(0);
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public void update(Setting setting) {
		Setting _setting = settingRepository.findOne(setting.getId());
		BeanUtils.copyProperties(setting,_setting);
		settingRepository.save(_setting);

	}

	@Override
	public void update(SettingWebsite settingWebsite) {
		SettingWebsite _settingWebsite = settingWebsiteRepository.findOne(settingWebsite.getId());
		BeanUtils.copyProperties(settingWebsite,_settingWebsite);
		settingWebsiteRepository.save(_settingWebsite);
	}

	@Override
	public void update(SettingEmail settingEmail) {
		SettingEmail _settingEmail = settingEmailRepository.findOne(settingEmail.getId());
		BeanUtils.copyProperties(settingEmail,_settingEmail);
		settingEmailRepository.save(_settingEmail);
	}

    @Override
    public void update(SettingCoin settingCoin) {
        SettingCoin _settingCoin = settingCoinRepository.findOne(settingCoin.getId());
        BeanUtils.copyProperties(settingCoin,_settingCoin);
        settingCoinRepository.save(_settingCoin);
    }

    @Override
    public void update(SettingCredit settingCredit) {
        SettingCredit _settingCredit = settingCreditRepository.findOne(settingCredit.getId());
        BeanUtils.copyProperties(settingCredit,_settingCredit);
        settingCreditRepository.save(_settingCredit);
    }

    @Override
    public void update(SettingTime settingTime) {
        SettingTime _settingTime = settingTimeRepository.findOne(settingTime.getId());
        BeanUtils.copyProperties(settingTime,_settingTime);
        settingTimeRepository.save(_settingTime);
    }

    @Override
    public void update(SettingIrrigation settingIrrigation) {
        SettingIrrigation _settingIrrigation = settingIrrigationRepository.findOne(settingIrrigation.getId());
        BeanUtils.copyProperties(settingIrrigation,_settingIrrigation);
        settingIrrigationRepository.save(_settingIrrigation);
    }

    @Override
    public void update(SettingSearch settingSearch) {
        SettingSearch _settingSearch = settingSearchRepository.findOne(settingSearch.getId());
        BeanUtils.copyProperties(settingSearch,_settingSearch);
        settingSearchRepository.save(_settingSearch);
    }

    @Override
    public void update(SettingSeo settingSeo) {
        SettingSeo _settingSeo = settingSeoRepository.findOne(settingSeo.getId());
        BeanUtils.copyProperties(settingSeo,_settingSeo);
        settingSeoRepository.save(_settingSeo);
    }

    @Override
    public void update(SettingOauth settingOauth) {
        SettingOauth _settingOauth = settingOauthRepository.findOne(settingOauth.getId());
        BeanUtils.copyProperties(settingOauth,_settingOauth);
        settingOauthRepository.saveAndFlush(settingOauth);
    }

    @Override
    public void update(SettingRegister settingRegister) {
	    SettingRegister _settingRegister = settingRegisterRepository.findOne(settingRegister.getId());
        BeanUtils.copyProperties(settingRegister,_settingRegister);
        settingRegisterRepository.save(_settingRegister);
    }

    @Override
    public void update(SettingCreditAndCoinInfo settingCreditAndCoinInfo) {
        this.update(settingCreditAndCoinInfo.getCredit());
        this.update(settingCreditAndCoinInfo.getCoin());
    }
}
